<template>
  <div class="row mt-5 mb-4 border rounded shadow-sm">
    <div class="col-md-12" style="padding-top: 10px; background-color: #dee2e6">
      <h3>
        <i class="fas fa-bug"></i>
        <span>{{cveID}}</span>
        Detail
        <span class="badge badge-success" style="position: absolute;right:10px">{{entityType}}</span>
      </h3>
    </div>
    <div class="col-md-12 p-3">
      <div class="row">
        <div class="col-12">
          <h4>Current Description (from MITRE)</h4>
          <span v-html="description"></span>
        </div>
        <div class="col-12">
          <button class="btn btn-warning" style="float: right"
                  v-on:click="analyzeDescription">Analyze!
          </button>
        </div>
      </div>
      <div class="row mt-3">
        <div class="col-12">
          <h4>Impact</h4>
          <div class="row border shadow-sm ml-3 mr-3">
            <div class="col-6">
              <div v-if="cvss2MetricData.notEmpty">
                <h5>CVSS 2.0 Metrics</h5>
                <strong>Base Score: </strong><span>{{cvss2MetricData.baseScore}}</span><span> {{cvss2MetricData.severity}}</span>
                <br>
                <strong>Vector: </strong><span>{{cvss2MetricData.vectorString}}</span>
                <br>
                <strong>Impact Subscore: </strong><span>{{cvss2MetricData.impactScore}}</span>
                <br>
                <strong>Exploitability
                  SubScore: </strong><span>{{cvss2MetricData.exploitAbilityScore}}</span>
                <hr>
                <strong>Access Vector (AV): </strong>{{cvss2MetricData.accessVector}}
                <br>
                <strong>Access Complexity (AC): </strong>{{cvss2MetricData.accessComplexity}}
                <br>
                <strong>Authentication (AU): </strong>{{cvss2MetricData.authentication}}
                <br>
                <strong>Confidentiality (C): </strong>{{cvss2MetricData.confidentialityImpact}}
                <br>
                <strong>Integrity (I): </strong>{{cvss2MetricData.integrityImpact}}
                <br>
                <strong>Availability (A): </strong>{{cvss2MetricData.availabilityImpact}}
                <br>
              </div>
            </div>
            <div class="col-6">
              <div v-if="cvss3MetricData.notEmpty">
                <h5>CVSS 3.0 Metrics</h5>
                <strong>Base Score: </strong><span>{{cvss3MetricData.baseScore}}</span><span> {{cvss3MetricData.baseSeverity}}</span>
                <br>
                <strong>Vector: </strong><span>{{cvss3MetricData.vectorString}}</span>
                <br>
                <strong>Impact Score: </strong><span>{{cvss3MetricData.impactScore}}</span>
                <br>
                <strong>Exploitability
                  Score: </strong><span>{{cvss3MetricData.exploitAbilityScore}}</span>
                <hr>
                <strong>Attack Vector (AV): </strong>{{cvss3MetricData.attackVector}}
                <br>
                <strong>Attack Complexity (AC): </strong>{{cvss3MetricData.attackComplexity}}
                <br>
                <strong>Privileges Required (PR): </strong>{{cvss3MetricData.privilegesRequired}}
                <br>
                <strong>User Interaction (UI): </strong>{{cvss3MetricData.userInteraction}}
                <br>
                <strong>Scope (S): </strong>{{cvss3MetricData.scope}}
                <br>
                <strong>Confidentiality (C): </strong>{{cvss3MetricData.confidentialityImpact}}
                <br>
                <strong>Integrity (I): </strong>{{cvss3MetricData.integrityImpact}}
                <br>
                <strong>Availability (A): </strong>{{cvss3MetricData.availabilityImpact}}
                <br>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="row mt-3">
        <div class="col-12">
          <h4>Known Affected Products</h4>
          <table class="table table-bordered table-hover">
            <tr>
              <th>Vendor</th>
              <th>Product</th>
              <th>Version</th>
              <th>Update</th>
              <th>Edition</th>
              <th>Type</th>
            </tr>
            <tbody>
            <tr v-for="(ap,index) in aps" :key="index">
              <td>{{ap.vendor}}</td>
              <td>{{ap.productName}}</td>
              <td>{{ap.version}}</td>
              <td>{{ap.update}}</td>
              <td>{{ap.edition}}</td>
              <td>
                <span class="badge badge-info">{{ap.type}}</span>
              </td>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
      <div class="row mt-3">
        <div class="col-12">
          <h5>Vulnerability Type</h5>
          <ul>
            <li>{{weaknessDetailInfo.name}} ({{weaknessDetailInfo.cweId}})</li>
          </ul>
        </div>
      </div>
      <div class="row mt-3">
        <div class="col-12">
          <h5>References to Other Information</h5>
          Please selecting the following address to see more information about this...
          <table class="table table-bordered table-hover">
            <tr>
              <th>Source</th>
              <th>Website address</th>
              <th>tags</th>
            </tr>
            <tbody>
            <tr v-for="(ref,index) in refs" :key="index">
              <td>{{ref.refsource}}</td>
              <td><a :href="ref.url">{{ref.url}}</a></td>
              <td>
                <span class="badge badge-info">{{ref.tags}}</span>
              </td>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import PubSub from 'pubsub-js'
import { reqNerForVulDescription } from '../../api/api-index'

export default {
  name: 'VulEntity',
  data () {
    return {
      cveID: '',
      entityType: '',
      description: '',
      cvss3MetricData: {
        notEmpty: false,
        baseScore: '',
        baseSeverity: '',
        severity: '',
        vectorString: '',
        impactScore: '',
        exploitAbilityScore: '',
        attackVector: '',
        attackComplexity: '',
        privilegesRequired: '',
        userInteraction: '',
        scope: '',
        confidentialityImpact: '',
        integrityImpact: '',
        availabilityImpact: ''
      },
      cvss2MetricData: {
        notEmpty: false,
        baseScore: '',
        severity: '',
        vectorString: '',
        impactScore: '',
        exploitAbilityScore: '',
        accessVector: '',
        accessComplexity: '',
        authentication: '',
        confidentialityImpact: '',
        integrityImpact: '',
        availabilityImpact: ''
      },
      aps: null,
      refs: null,
      weaknessDetailInfo: { cweId: '', name: '' }
    }
  },
  mounted () {
    PubSub.subscribe('vulData', (msg, data) => {
      this.cveID = data.cveId
      this.entityType = 'vulnerability'
      this.description = data.description

      if (data.nvdBaseMetricV2 != null) {
        this.cvss2MetricData = data.nvdBaseMetricV2
        this.cvss2MetricData.notEmpty = true
      }
      if (data.nvdBaseMetricV3 != null) {
        this.cvss3MetricData = data.nvdBaseMetricV3
        this.cvss3MetricData.notEmpty = true
      }

      this.aps = data.vulnerabilityProductList

      this.refs = data.vulnerabilitySourceList
    })

    PubSub.subscribe('weaknessData', (msg, data) => {
      this.weaknessDetailInfo = data
    })
  },
  methods: {
    analyzeDescription () {
      reqNerForVulDescription(this.description).then(response => {
        this.description = response
      })
    }
  }
}
</script>

<style scoped>

</style>
